﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.Drawing;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS_运价运算
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMSWayBillOrderPriceYsController : ControllerBase
    {
        private readonly ILogger<TMSWayBillOrderPriceYsRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public TMSWayBillOrderPriceYsController(ILogger<TMSWayBillOrderPriceYsRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");

        }

        /// <summary>
        /// TMS_运价运算
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostOrderPriceYs([FromBody] TMSWayBillOrderPriceYsRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMSWayBillOrderPriceYsResponseDto re = new TMSWayBillOrderPriceYsResponseDto();
            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic

            if (strparm.row.Count<=0)
            {
                strMsg = "计划明细表为空，运算失败！";
            }
            if (strparm.carrier_code==""|| strparm.carrier_code==null)
            {
                strMsg = "承运商编码为空，运算失败！";
            }
            if (strparm.cartype_code == "" || strparm.cartype_code == null)
            {
                strMsg = "车型编码为空，运算失败！";
            }


            try
            {
                if (strMsg=="")
                {


                    decimal yprice = 0;//最终价格
                    string price_no = "";//最终SRM价格

                    foreach (var item in strparm.row)
                    {
                        decimal price = 0;
                        string price_no_d = "";//SRM报价流水号

                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("start_address", item.shipping_city);
                        dic.Add("end_address", item.delivery_city);
                        dic.Add("cartype_code", strparm.cartype_code);
                        dic.Add("carrierid_code", strparm.carrier_code);
                        DataTable dt_price = MethodHelp.GetTableInfo("TMS0167", dic, con);//固定价，正向获取
                        if (dt_price.Rows.Count > 0)
                        {
                            price = DB.ToDecimal(dt_price.Rows[0]["tax_price"].ToString());
                            price_no_d = dt_price.Rows[0]["price_no"].ToString();
                        }
                        else
                        {
                            DataTable dt_price_wf = MethodHelp.GetTableInfo("TMS0168", dic, con);//固定价，反向获取
                            if (dt_price_wf.Rows.Count > 0)
                            {
                                price = DB.ToDecimal(dt_price_wf.Rows[0]["tax_price"].ToString());
                                price_no_d = dt_price_wf.Rows[0]["price_no"].ToString();
                            }
                        }
                        if (yprice == 0)
                        {
                            yprice = price;
                            price_no = price_no_d;
                        }
                        else
                        {
                            //拼车取价格小值
                            if (yprice >= price)
                            {
                                yprice = price;
                                price_no = price_no_d;
                            }
                        }
                    }

                    if (price_no!="")
                    {
                        double tax_price = 0;
                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("price_no", price_no);
                        DataTable dt_price = MethodHelp.GetTableInfo("TMS0169", dic, con);//获取SRM运价信息
                        if (dt_price.Rows.Count>0)
                        {
                            //往返同价除2
                            if (dt_price.Rows[0]["price_type"].ToString()== "往返总价")
                            {
                                tax_price = DB.ToDouble(dt_price.Rows[0]["tax_price"].ToString()) / 2;
                            }
                            else
                            {
                                //正常就不用除
                                tax_price = DB.ToDouble(dt_price.Rows[0]["tax_price"].ToString());
                            }



                            re.srm_priceno = dt_price.Rows[0]["price_no"].ToString();
                            re.item_code = dt_price.Rows[0]["item_code"].ToString();
                            re.item_name = dt_price.Rows[0]["item_name"].ToString();
                            re.compute_ploy = dt_price.Rows[0]["compute_ploy"].ToString();
                            re.compute_mode = dt_price.Rows[0]["compute_mode"].ToString();
                            re.begin_range = dt_price.Rows[0]["begin_range"].ToString();
                            re.stop_range = dt_price.Rows[0]["stop_range"].ToString();
                            re.tax_price = tax_price;
                            re.price_amount = tax_price;

                        }
                        else
                        {
                            strMsg = "获取SRM报价流水号=<"+ price_no + "信息失败，请联系管理员！";
                        }


                    }
                    else
                    {
                        strMsg = "获取运价失败，请手动选择SRM报价明细！";
                    }
                }
           

            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
                    
            if (strMsg=="")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
